// 角色管理相关的接口
import request from '@/utils/request'
import { AllPermissionResponseData, AllRoleResponseData } from './type'

enum API {
  // 分页查询角色列表
  ALLROLE_API = "/admin/acl/role/",
  // 新增角色
  ADDROLE_URL = "/admin/acl/role/save",
  // 修改角色
  UPDATEROLE_URL = "/admin/acl/role/update",
  // 根据角色id获取该角色的所有权限
  ALLPERMISSION_API = "/admin/acl/permission/toAssign/",
  // 分配权限
  SETPERMISSION_API = "/admin/acl/permission/doAssignAcl",
  // 删除角色
  DELETEROLE = "/admin/acl/role/remove/",
}
// 分页查询角色列表
export const reqAllRole = (currentPage: number, pageSize: number, roleName: string) =>
  request.get<any, AllRoleResponseData>(API.ALLROLE_API + `${currentPage}/${pageSize}?roleName=${roleName}`)
// 新增或修改角色
export const reqAddOrUpdateRole = (data: any) => {
  if (data.id) {
    return request.put<any, any>(API.UPDATEROLE_URL, data)
  } else {
    return request.post<any, any>(API.ADDROLE_URL, data)
  }
}
// 根据角色id获取该角色的所有权限
export const reqAllPermissionById = (id: number) =>
  request.get<any, AllPermissionResponseData>(API.ALLPERMISSION_API + id)
// 删除角色
export const reqDeleteRoleById = (id: number) =>
  request.delete<any, any>(API.DELETEROLE + id)
// 分配权限
export const reqSetPermission = (roleId: number, permissionId: number[]) =>
  request.post<any, any>(API.SETPERMISSION_API, { roleId, permissionIdList: permissionId })
